package com.bana.fvs.app.module.login.page;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.common.ConstantsField;
import com.bana.fvs.app.enter.SetBaseUrlActivity;
import com.bana.fvs.app.main.entity.UserInfoBean;
import com.bana.fvs.app.main.page.MainActivity;
import com.bana.fvs.app.mvp.contract.LoginContract;
import com.bana.fvs.app.mvp.presenter.LoginPresenter;
import com.bana.fvs.app.util.UserInfoUtils;
import com.bana.fvs.app.view.PassWordInputLayout;
import com.wei.commonlibrary.net.CommException;
import com.wei.commonlibrary.utils.GlobalContext;
import com.wei.commonlibrary.utils.SPUtils;
import com.wei.commonlibrary.utils.ToastUtil;

import butterknife.BindView;

/**
 * Author : yanftch
 * Date   : 2018/3/10
 * Time   : 14:21
 * Desc   : 登录
 */

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.ILoginView {
    private static final String TAG = "dah_LoginActivity";
    @BindView(R.id.loginEtAccount)
    EditText account;
    @BindView(R.id.LoginInputPassword)
    PassWordInputLayout password;
    @BindView(R.id.login_tv)
    TextView login;
    @BindView(R.id.tvErrorInfo)
    TextView tvErrorInfo;
    @BindView(R.id.et_base_url_tv)
    TextView etBaseUrlTv;
    @BindView(R.id.login_iv_pwd)
    ImageView btn_pwd;
    private String save_username, save_pwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.setStatusBarColor(Color.WHITE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    @Override
    public int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void setTitle() {
        mBaseTitleBarView.setBaseTitleBarViewGone();
    }

    @Override
    public void initWidget() {
        btn_pwd.setOnClickListener(this);
        login.setOnClickListener(this);
        etBaseUrlTv.setOnClickListener(this);
        setLoginStyle();
        account.addTextChangedListener(new AccountWatcher());
        password.addOnEditTextChangeListener(new PassWordInputLayout.onEditTextChangeListener() {
            @Override
            public void editTextChangeListener(Editable editable) {
                setLoginStyle();
            }
        });

        save_username = SPUtils.getStringData(GlobalContext.context, ConstantsField.USERNAME);
        account.setText(save_username);
        save_pwd = SPUtils.getStringData(GlobalContext.context, ConstantsField.PWD);

        //是否记住密码
        Boolean isSave = SPUtils.getBooleanData(GlobalContext.context, ConstantsField.ISSAVA);
        btn_pwd.setSelected(isSave);
        if (isSave) {
            password.setmPassword(save_pwd);
        }
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.login_iv_pwd:
                btn_pwd.setSelected(!btn_pwd.isSelected());
                break;
            case R.id.login_tv:
                if (TextUtils.isEmpty(account.getText().toString())) {
                    ToastUtil.showLong(mActivity, getString(R.string.login_name_null));
                    return;
                }
                if (TextUtils.isEmpty(password.getPasswordText())) {
                    ToastUtil.showShort(GlobalContext.context, getString(R.string.login_pwd_null));
                    return;
                }
                http_login();
                break;
            case R.id.et_base_url_tv:
                startActivity(SetBaseUrlActivity.class);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initPresenter() {
        mPresenter.setView(this);
    }

    /**
     * 登录接口
     */
    private void http_login() {
        mPresenter.http_login(mActivity, account.getText().toString(), password.getPasswordText());
    }

    /**
     * 实时控制【登录】按钮样式
     */
    private void setLoginStyle() {
        if (TextUtils.isEmpty(account.getText().toString()) &&
                TextUtils.isEmpty(password.getPasswordText())) {
            login.setEnabled(false);
            login.setBackgroundResource(R.drawable.shape_round_cerners_bg_enable);
        } else {
            login.setEnabled(true);
            login.setBackgroundResource(R.drawable.shape_round_cerners_bg);
        }
    }

    /**
     * 显示登录错误信息
     *
     * @param show TRUE-显示
     */
    private void showErrorInfo(boolean show) {
        tvErrorInfo.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
    }

    @Override
    public void onError(CommException e) {
        ToastUtil.showShort(mContext, e.getMessage());
        showErrorInfo(true);
    }

    @Override
    public void onResponse(UserInfoBean bean) {
        if (null != bean) {
            Log.e(TAG, "onResponse: " + bean.toString());
            showErrorInfo(false);
            startActivityAndFinish(MainActivity.class);
            UserInfoUtils.getInstance().saveUserInfo(GlobalContext.context, bean);
        } else {
            showErrorInfo(true);
        }
    }

    private class AccountWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            setLoginStyle();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        String username = account.getText().toString();
        SPUtils.saveStringData(GlobalContext.context, ConstantsField.USERNAME, username);
        SPUtils.saveBooleanData(GlobalContext.context, ConstantsField.ISSAVA, btn_pwd.isSelected());
        if (btn_pwd.isSelected()) {
            SPUtils.saveStringData(GlobalContext.context, ConstantsField.PWD, password.getPasswordText());
        } else {
            SPUtils.removeData(GlobalContext.context, ConstantsField.PWD);
        }
    }
}
